package com.rongkang.interview.jvm.quote;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

/**
 * @Description:
 * @author: 荣康
 * @date: 2021/12/17
 */
public class PhantomReferenceDemo {
    public static void main(String[] args) {
        Object o1 = new Object();
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
        PhantomReference<Object> phantomReference = new PhantomReference<>(o1, referenceQueue);
        System.out.println("o1 = " + o1);
        System.out.println("phantomReference = " + phantomReference.get());
        //取队列中的内容
        System.out.println("referenceQueue = " + referenceQueue.poll());
        o1 = null;
        System.gc();
        System.out.println("o1 = " + o1);
        System.out.println("phantomReference = " + phantomReference.get());
        System.out.println("referenceQueue = " + referenceQueue.poll());
    }
}
